var jobarray;
var sURL = unescape(window.location.pathname);
var checkJobState;
var servername;
var job_id;
var numjobs;

function doLoad() {
     setTimeout( "refresh()", 30*1000 );
}

function callAjax(server) {

    var jobstring;
    var joblist;
    var num;
    var recs;

    var a;

    rescan = 0;

    jobstring = readCookie(server);
    joblist   = jobstring.split(',');

    for(a = 0; a < joblist.length; a++) {
        info   = joblist[a].split(':');
        do_expandJobInfo(info[0], info[1], 0);
    }

}

function refresh() {
    window.location.reload( false );
}

function jobinfo_cb(x) {

    var start;
    var end;

    var tempjob;
    var realjobid;

    start = x.indexOf('jobname');
    end   = x.indexOf('&jobno=');

    job = x.substring(start,end);

    realjobid = job.split('=');

    job_id = realjobid[1] + "_id";

    document.getElementById(job_id).setAttribute("length", x.length);
    document.getElementById(job_id).innerHTML = x;

    if (checkJobState == 1) {
        setJobstate();
    } 

}

function do_expandJobInfo(id, recs, rescan) {

    var jobname;
    var server;

    checkJobState = rescan;

    job_id = id;

    jobname = document.getElementById(job_id).getAttribute("jobname");
    server  = document.getElementById(job_id).getAttribute("server");
    size    = document.getElementById(job_id).getAttribute("length");
    valid   = isNumeric(document.getElementById('toshow'), 
                                                'Numeric Entries Only');
    if (recs == 0) {
        numelement = document.getElementById('toshow');
        num        = numelement.value;
    } else {
        num = recs;
    }

    numjobs = num;

    if (valid == false) {
        return false;
    }

    document = document.getElementById(job_id).innerHTML;

    if (size > 0) {
        document.getElementById(job_id).innerHTML = '';
        document.getElementById(job_id).setAttribute("length", 0);
        if (checkJobState == 1) {
            setJobstate();
        }
        return;
    }

    document.getElementById(job_id).innerHTML = "<div id='loading' style='position:absolute; width:100%; text-align:center; top:300px;'><img src=/cabieicons/loading.gif border=0></div>";

    servername = server;

    x_ajax_jobinfo(server, jobname, num, jobinfo_cb);

}

function isNumeric(elem, helperMsg){

    var numericExpression = /^[0-9]+$/;

    if(elem.value.match(numericExpression)){
        createCookie('numjobs', elem.value);
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }

}

function setJobstate() {

    var tags = document.getElementsByTagName('tbody');
    var e = 0;
    var found;

    jobarray   = new Array();
    joblist    = new Array();
    countarray = new Array();

    jobstring = readCookie(servername);

    for(a = 0; a < tags.length; a++) {

        if (tags[a].className == 'collapse_obj') {
            size = document.getElementById(tags[a].id).getAttribute("length");
            if (size > 0) {

                found = 0;
  
                if (jobstring != undefined) {

                    joblist = jobstring.split(',');

                    for(b = 0; b < joblist.length; b++) {

                        search = joblist[b].split(':');

                        if (tags[a].id == search[0]) {
                            jobarray[e] = search[0] + ":" + search[1];
                            found = 1;
                        }

                    }

                } 

                if (found == 0) {
                    jobarray[e] = tags[a].id + ":" + numjobs;
                }

                e++;

            }

        }

    }

    if (jobarray.length > 0) {
        createCookie(servername, jobarray);
    } else {
        eraseCookie(servername);
    }

}

function getDisplay(url){

    num    = document.getElementById('toshow');
    toshow = num.value;

    document.location.href=url+toshow;

}

function createCookie(name,value,days) {

    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else  {
        var expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name) {

    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length,c.length);
            }
        }
    }

    return null;

}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
